import traceback
import logging
import os
import sys
import json

from app import create_app, create_asgi_app
from configs.settings import flask_config
from tools.apscheduler_tools import scheduler
app = create_app()
asgi_app = create_asgi_app(app)



if __name__ == "__main__":
    if sys.argv[1] == "runserver":
        app.run(
            host=flask_config.HOST, port=flask_config.PORT, debug=flask_config.DEBUG
        )
    else:
        os.system(f"flask {' '.join(sys.argv[1:])}")
        app.logger.warning("建议请使用 `flask --help` 中支持的命令操作，而不是使用manage.py")


if __name__ != "__main__":
    from configs.log_config import DatabaseLogHandler

    gunicorn_logger = logging.getLogger("gunicorn.error")

    dbh = DatabaseLogHandler()
    dbh.setLevel(logging.WARNING)
    gunicorn_logger.handlers.append(dbh)

    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)
